//answer.js
let {
  api_GetTopicAnswer,
  api_AddTopicFavourate,
  api_MakeComment,
  api_MakeAnswer,
  api_SubTopicFavourate
} = require("../../api/getData.js")

const app = getApp()

Page({
  data: {
    favourateCnt: 0,
    username: "",
    topicTitle: "",
    topicAnswerCnt: 0,
    topicFavourateCnt: 0,
    topicInfo: [],
    topicAnswerInfo:[],
    haveAddFavourate:false,

    answerType: 0,
    answerComment: "",
    answerUsername: "",
    answerid: "",
    commentid: -1,
    content: "",

    disable: false,
    

    imageFavourateUrl:"../../../images/luntan/love_blue.png",

    //模态框
    hideModal: true, //模态框的状态  true-隐藏  false-显示
    animationData: {},
  },

  setContent: function (e) {
    this.setData({
      content: e.detail.value
    })
  },

  bindMakeMainComment: function (e) {
    console.log('主评论被加载')
    var commentData = e.currentTarget.dataset.index;
    var dataTmp = JSON.stringify(commentData.main_comment);
    var topicInfo = JSON.stringify(this.data.topicInfo);
    this.setData({
      answerComment: commentData.main_comment.comment,
      answerUsername: commentData.main_comment.username,
      commentid:-1,
      answerType: 1,
      answerid: commentData.main_comment.id,
    })
    this.showModal()
  },

  bindMakefollowComment: function (e) {
    var commentData = e.currentTarget.dataset.index;
    var dataTmp = JSON.stringify(commentData);
    var topicInfo = JSON.stringify(this.data.topicInfo);
    this.setData({
      answerComment: commentData.comment,
      answerUsername: commentData.username,
      commentid: commentData.id,
      answerType: 2,
      answerid: commentData.answerId,
    })
    this.showModal()
  },

  bindMakeAnswer: function () {
    var commentData = this.data.topicInfo
    var dataTmp = JSON.stringify(commentData);
    var topicInfo = JSON.stringify(this.data.topicInfo);
    this.setData({
      answerComment: this.data.topicInfo.title,
      answerUsername: this.data.topicInfo.username,
      answerType: 3,
    })
    this.showModal()
  },

  makeComment() {
    if(this.data.disable==true){
      return;
    }
    this.setData({
      disable:true,
    })
    if (this.data.answerType == 3) {
      let commentData = {
        userid: app.globalData.userInfo.id,
        comment: this.data.content,
        topicid: this.data.topicInfo.id,
      }
      api_MakeAnswer(commentData, (res) => {
        if (res.status == 200) {
          wx.showToast({
            title: '发表成功',
            duration: 100000,
          })
        }
        else {
          wx.showToast({
            title: '发表失败',
            duration: 100000,
          })
        }
        var topicInfo = this.data.topicInfo;
        topicInfo.answerCnt = topicInfo.answerCnt + 1;
        var dataTmp = JSON.stringify(topicInfo);
        setTimeout(function () {
          wx.redirectTo({
            url: '../../mine/question/question?topic=' + dataTmp
          })
        }, 2000)
      })
    } else {
      let commentData = {
        userId: app.globalData.userInfo.id,
        commentId: this.data.commentid,
        type: this.data.answerType,
        comment: this.data.content,
        answerid: this.data.answerid,
        commentUsername: this.data.answerUsername,
      }
      api_MakeComment(commentData, (res) => {
        if (res.status == 200) {
          wx.showToast({
            title: '发表成功',
            duration: 100000,
          })
        }
        else {
          wx.showToast({
            title: '发表失败',
            duration: 100000,
          })
        }
        var topicInfo = JSON.stringify(this.data.topicInfo);
        setTimeout(function () {
          wx.redirectTo({
            url: '../../mine/question/question?topic=' + topicInfo
          })
        }, 2000)
      })
    }
  },

  bindAddFavourateCnt: function(){
    if (this.data.haveAddFavourate){
      console.log("已经点过赞了");
      var tmp = this.data.topicInfo;
      tmp.favourateCnt = tmp.favourateCnt + 1;
      this.setData({
        imageFavourateUrl: "../../../images/luntan/love_blue.png",
        topicFavourateCnt: this.data.topicFavourateCnt - 1,
        topicInfo: tmp,
        haveAddFavourate: false,
      })
      var data = {
        topicid: this.data.topicInfo.id,
      }
      api_SubTopicFavourate(data, (res) => {
        
      })
    }else{
      var tmp = this.data.topicInfo;
      tmp.favourateCnt = tmp.favourateCnt + 1;
      this.setData({
        imageFavourateUrl: "../../../images/luntan/love_red.png",
        topicFavourateCnt: this.data.topicFavourateCnt + 1,
        topicInfo: tmp,
        haveAddFavourate: true,
      })
      var data = {
        topicid: this.data.topicInfo.id,
      }
      api_AddTopicFavourate(data, (res) => {

      })
    }
  },

  bindImgYulan: function (event) {
    var src = event.currentTarget.dataset.src;//获取data-src
    wx.previewImage({
      current: src, // 当前显示图片的http链接
      urls: this.data.topicInfo.imgUrlList
    })
  },



  onLoad: function (options) {
    let params = JSON.parse(options.topic);
    console.log('收到数据', params.id)
    this.setData({
      topicInfo:params,
      topicFavourateCnt: params.favourateCnt,
      topicAnswerCnt: params.answerCnt,
    })
    api_GetTopicAnswer(params.id, (res) => {
      var answerList = []
      console.log(res.data.data)
      res.data.data.forEach(i => {
        answerList.push(i)
      })
      this.setData({
        topicAnswerInfo:answerList
        
      })
    })
  },
  tapName: function(event){
    console.log(event)
  },

  onShareAppMessage:function(res){
    if(res.form=='button'){
        console.log(res.target)
    }
    return{
      title: '自定义转发标题',
      path: '/pages/index/index'
    }
  },



  showModal: function (e) {
    console.log("打开模态框")
    var that = this;
    that.setData({
      hideModal: false,
    })
    var animation = wx.createAnimation({
      duration: 600,//动画的持续时间 默认400ms   数值越大，动画越慢   数值越小，动画越快
      timingFunction: 'ease',//动画的效果 默认值是linear
    })
    this.animation = animation
    setTimeout(function () {
      that.fadeIn();//调用显示动画
    }, 200)
  },

  // 隐藏遮罩层
  hideModal: function () {
    var that = this;
    var animation = wx.createAnimation({
      duration: 800,//动画的持续时间 默认400ms   数值越大，动画越慢   数值越小，动画越快
      timingFunction: 'ease',//动画的效果 默认值是linear
    })
    this.animation = animation
    that.fadeDown();//调用隐藏动画   
    setTimeout(function () {
      that.setData({
        hideModal: true
      })
    }, 720)//先执行下滑动画，再隐藏模块

  },

  //动画集
  fadeIn: function () {
    this.animation.translateY(0).step()
    this.setData({
      animationData: this.animation.export()//动画实例的export方法导出动画数据传递给组件的animation属性
    })
  },
  fadeDown: function () {
    this.animation.translateY(300).step()
    this.setData({
      animationData: this.animation.export(),
    })
  },

})
